Ext.setup( {
	icon : 'icon.png',
	tabletStartupScreen : 'tablet_startup.png',
	phoneStartupScreen : 'phone_startup.png',
	glossOnIcon : false,
	onReady : function() {
		var form;	
		
        Ext.regModel('FixedAssetsPurchaseApplyData', {
            fields: [
                {name: 'COMMITER',     type: 'string'},
                {name: 'DEPARTMENT', type: 'string'},
                {name: 'ASSETNAME', type: 'string'},
                {name: 'ASSETTYPE', type: 'string'},
                {name: 'ASSETMODEL', type: 'string'},
                {name: 'ASSETPRICE', type: 'string'},
                {name: 'ASSETQUANTITY', type: 'string'},
                {name: 'UNIT', type: 'string'},
                {name: 'DESCRIBE', type: 'string'}
            ],
        validations: [ 
   	               	  {type: 'presence', name: 'ASSETNAME'},
		              {type: 'presence', name: 'ASSETTYPE'},
		              {type: 'presence', name: 'ASSETMODEL'},
		              {type: 'presence', name: 'ASSETPRICE'},
		              {type: 'presence', name: 'ASSETQUANTITY'},
		              {type: 'presence', name: 'UNIT'},
		              {type: 'presence', name: 'DESCRIBE'},
		          ]
        });
        
		var agenymapStore = new Ext.data.Store({
			autoLoad: true,
		    model: 'agenymapmodel',
		    autoLoad: true,
		    proxy: {
		        type: 'ajax',
		        url : '../dic/dic_agencyMap.action',
		        reader: {
		            type: 'json'
		        }
		    }
		});
		
		Ext.Ajax.request({
             url: '../fixedAssets/fixedAssetsApply_loadAdd.action?COMMITERCODE='+window.location.href.split("=")[1],
             success: function(response, opts) {
            	 var res=JSON.parse(response.responseText);
            	 console.log(res);
            	 formBase.fixedAssetsPurchaseApplyData = Ext.ModelMgr.create({
                     'COMMITER' : res.COMMITER,
                     'DEPARTMENT': res.DEPARTMENT
                 },  'FixedAssetsPurchaseApplyData');

                 form.loadModel(formBase.fixedAssetsPurchaseApplyData);
             }
         });
		
		var formBase = {
			scroll : 'vertical',
			standardSubmit : false,
			items : [
			         {
				xtype : 'fieldset',
				title : '所属单位',
                items:[
                       {
                           xtype:'selectfield',
                           name:'OWNERDEPT',
                           label:'单位名称',
   						xtype : 'selectfield',
   						name : 'AGENCYID',
   						label : '单位名称',
   						placeHolder : '请选择所属单位',
   						valueField: 'AGENCYID',
   						displayField:'NAME',
   						store : agenymapStore,
   						required : true
                       }
                   ]
			},
			{
				xtype : 'fieldset',
				title : '基本信息',
				instructions : '请输入申请人信息',
				defaults : {
					required : true,
					labelAlign : 'left',
					labelWidth : '40%'
				},
				items : [
				         {
							xtype : 'textfield',
							name : 'COMMITER',
							label : '申请人',
							value : '',
							useClearIcon : false
				         }, 
				         {
							xtype : 'textfield',
							name : 'DEPARTMENT',
							label : '申请人单位',
							value : '',
							useClearIcon : false
				         } 
		         ]
			}, 
			{
				xtype : 'fieldset',
				title : '详细信息',
				defaults : {
					required : true,
					xtype : 'fieldset'
				},
				items : [   {
					xtype : 'selectfield',
					name : 'ASSETTYPE',
					label : '资产类别',
					options : [ {
						text : '请选择资产类别',
						value : '0'
					}, {
						text : '打印机',
						value : '1'
					}, {
						text : '显示器',
						value : '2'
					}, {
						text : '台式机电脑',
						value : '3'
					}, {
						text : '笔记本电脑',
						value : '4'
					}  ]
				},
		         {
					xtype : 'textfield',
					name : 'ASSETNAME',
					label : '资产名称',
					value : '',
					useClearIcon : false
		         }, 
		         {
						xtype : 'textfield',
						name : 'ASSETMODEL',
						label : '资产型号',
						value : '',
						useClearIcon : false
		         }, 
		         {
						xtype : 'textfield',
						name : 'ASSETPRICE',
						label : '资产单价',
						value : '',
						useClearIcon : false
		         }, 
		         {
						xtype : 'textfield',
						name : 'ASSETQUANTITY',
						label : '采购数量',
						value : '',
						useClearIcon : false
		         },
		         {
						xtype : 'textfield',
						name : 'UNIT',
						label : '单位',
						value : '',
						useClearIcon : false
		         },
				 {
					xtype : 'textareafield',
					name : 'DESCRIBE',
					label : '备注',
					maxLength : 60,
					maxRows : 3
				} ]
			} ],
			listeners : {
				submit : function(form, result) {
					console.log('success', Ext.toArray(arguments));
				},
				exception : function(form, result) {
					console.log('failure', Ext.toArray(arguments));
				}
			},

			dockedItems : [ {
				xtype : 'toolbar',
				dock : 'bottom',
				items : [ {
					xtype : 'spacer'
				}, {
					text : '重置',
					handler : function() {
						form.reset();
					}
				}, {
					text : '保存',
					ui : 'confirm',
					handler:function () {
						var currentNote = form.getRecord(); 
			            form.updateRecord(currentNote); 		 
		                var errors = currentNote.validate(); 
		                if (!errors.isValid()) { 
		                Ext.Msg.alert('等待...', "带*号选项为必填信息！", Ext.emptyFn); 
		                    return; 
		                } 
                    	Ext.Msg.confirm('系统提示','确定要提交？',function(btn){                    		
            		        if(btn=='yes'){  				            
  								var url="../fixedAssets/fixedAssetsApply_add.action?COMMITERCODE="+window.location.href.split("=")[1];
  								formBase.submitform(url);
            		        }else{
            		        	return;
            		        }
                    		        
                    	},this);
					}
				} ]
			} ],
			
            submitform:function(url){
            	form.submit( {
    				waitMsg : {
    					message : '正在提交...',
    					cls : 'demos-loading'
    				},
    			 url: url,           
    			 method: 'POST',           
    			 success: function(thisForm, action){ 
    					Ext.Msg.alert('提示', '提交成功',function(btn){
    						document['success'] = 'true';
    					});	
    					
    				}
    			});
            }
		};

		if (Ext.is.Phone) {
			formBase.fullscreen = true;
		} else {
			Ext.apply(formBase, {
				autoRender : true,
				floating : true,
				modal : true,
				centered : true,
				hideOnMaskTap : false,
				height : 385,
				width : 480
			});
		}

		form = new Ext.form.FormPanel(formBase);
		form.show();
	}
});